/*
 * Copyright (C) 2011 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.core.settings;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import ch.bfh.aeneas.exchange.intent.AeneasIntent;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 *
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class SettingsService extends Service {

    protected final String LOG_NAME = "SettingsManager";
    protected final String AVAILABLE_SETTINGS = "AvailableSettings";
    
    protected SharedPreferences mSettings;
    protected Editor mEditor;
    
    public class SettingsServiceBinder extends Binder {

        public SettingsService getService() {
            return SettingsService.this;
        }
    }    
    private final SettingsServiceBinder mBinder = new SettingsServiceBinder();

    private final BroadcastReceiver mRequestReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if(arg1.getAction().equalsIgnoreCase(AeneasIntent.ACTION_REQUEST)) {
                //if()
            } else if(arg1.getAction().equalsIgnoreCase(AeneasIntent.ACTION_STORE)) {
                
            }
        }        
    };
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate() {
        super.onCreate();
        
        IntentFilter infi = new IntentFilter(AeneasIntent.ACTION_REQUEST);
        infi.addCategory(AeneasIntent.CATEGORY_SETTINGS);
        getApplicationContext().registerReceiver(mRequestReceiver, infi);
        
        infi = new IntentFilter(AeneasIntent.ACTION_STORE);
        infi.addCategory(AeneasIntent.CATEGORY_SETTINGS);
        getApplicationContext().registerReceiver(mRequestReceiver, infi);
        
        mSettings = getSharedPreferences(AVAILABLE_SETTINGS,MODE_PRIVATE);
        mEditor = mSettings.edit();
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    
    private boolean hasSettings(int identifier) {
        return hasSettings(Integer.toString(identifier));
    }
    
    private boolean hasSettings(String identifier) {
        return mSettings.contains(identifier);
    }
    
    private Bundle getSettings(int id) {
        String identifier = Integer.toString(id);
        if(hasSettings(identifier)) {
            SharedPreferences settings = getSharedPreferences(identifier,MODE_PRIVATE);
            return bundleSettings(settings);
        }
        return new Bundle();
    }
    
    protected Bundle bundleSettings(SharedPreferences settings) {
        Bundle b = new Bundle();
        
        Map allSettings = settings.getAll();
        Set entries = allSettings.entrySet();
        Iterator i = entries.iterator();
        Map.Entry entry;
        
        while(i.hasNext()) {
            entry = (Map.Entry) i.next();
            if(!addToBundleByType(b,entry)) {
            }
        }        
        return b;
    }
    
    private Boolean addToBundleByType(Bundle b, Entry entry) {
        String name = (String) entry.getKey();
        Object value = entry.getValue();
        if(value instanceof Boolean) {
            b.putBoolean(name, (Boolean) value);
        }
        else if(value instanceof String) {
            b.putString(name, (String) value);
        }
        else if(value instanceof Float) {
            b.putFloat(name, (Float) value);
        }
        else if(value instanceof Long) {
            b.putLong(name, (Long) value);
        }
        else if(value instanceof Double) {
            b.putDouble(name, (Long) value);
        }
        else if(value instanceof Short) {
            b.putShort(name, (Short) value);
        }
        else if(value instanceof Parcelable) {
            b.putParcelable(name, (Parcelable) value);
        }
        else {
            return false;
        }
        return true;
    }
}